<?php
/**
	Sponsor Manager (class.domains.php file)
    Copyright (C) <Fri Apr 11 21:56:43 CEST 2008>  Marcos Ramírez <marcosramirez@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    
    Purpose:
    Last Modified:
**/

class domains {
	private static $result='false';
	
	public function __construct() {
		
	}
	
	public function addIp ($ip) {
		global $sm;
		global $html;
		if (!$resultado=$sm->existsOnTable('My_IPs', 'ip', $ip) && $ip!='')
			$this->insertIP($ip);
		else 
			$html->addError("IP en db");
		
	}

	public function addIPList ($ipList) {
		global $html;
		global $sm;
		$List=explode('<br />', nl2br($ipList));
		foreach ($List as $ip) {
			$ip=ltrim(trim($ip));
			if ($ip!='') {
				if (!$resultado=$sm->existsOnTable('My_IPs', 'ip', $ip)) {
					$this->insertIP($ip);
				}else {
					$html->addError("IP: $ip in database");
				}
			} 
		}
	}

	
	public function addDomain ($domain) {
		global $sm;
		global $html;
		if (!$resultado=$sm->existsOnTable('My_Domains', 'url', $domain))
			$this->insertDomain($domain);
		else 
			$html->addError("Dominio en db");
	}
	
	public function addDomainList ($domainlist) {
		global $html;
		global $sm;
		$List=explode('<br />', nl2br($domainlist));
		foreach ($List as $domain) {
			$domain=ltrim(trim($domain));
			if ($domain!='') {
				if (!$resultado=$sm->existsOnTable('My_Domains', 'url', $domain)) {
					$this->insertDomain($domain);
				}else {
					$html->addError("$domain in database");
				}
			} 
		}
	}

	private function insertDomain ($domain) {
		global $sm;
		global $html;
		$sm->Query("INSERT INTO My_Domains (url) VALUES ('$domain')");
		$html->addInfo($domain._(' correctly added.'));
	}

	private function insertIP ($ip) {
		global $sm;
		global $html;
		$sm->Query("INSERT INTO My_IPs (ip) VALUES ('$ip')");
		$html->addInfo(_('IP address: ').$ip._(' correctly added.'));
	}

	public function isMine ($domain) {
		$this->get_IP($domain);
		return $this->result;
	}
	
	private function isInDataBase ($domain) {
		global $sm;
		$resultado=$sm->existsOnTable('My_Domains', 'url', $domain);
		return $resultado;
	}
	
	private function get_IP($domain) {
		global $sm;
		global $Cfg;
		$ip=gethostbyname($domain);
		if ($ip==$domain)
			$ip='NO IP Found, Unregistered?';

		if (!$sm->existsOnTable('My_IPs', 'ip', $ip)) {
			return '<div class="notMineIp">'.$ip.'</div>';
		} else {
			if ($Cfg['Sys']['autoActivateDomainsIpMatching']==1)
				$this->activateDomain($domain);

			return '<div class="mineIp">'.$ip.'</div>';	
		}
		
	}
	
	public function getIp ($domain) {
		return $this->get_IP($domain);
	}
	
	public function getDomainList () {
		global $sm;
		$result=$sm->Query("SELECT url FROM My_Domains");
		return $Res=$sm->FetchAll($result);
	}
	
	public function checkDomain($domain) {
		return $this->get_IP($domain);
	}
	
	public  function countDomains() {
		global $sm;
		$result=$sm->Query("SELECT COUNT(url) FROM My_Domains");
		$res=$sm->Fetch($result);
		return $res['COUNT(url)'];
	}
	
	public function isActive ($domain) {
		global $sm;
		$result=$sm->Query("SELECT activo FROM My_Domains WHERE url='$domain'");
		$Res=$sm->Fetch($result);
		if ($Res['activo']=='1')
			return _('Yes');
		else 
			return _('No');
	}
	
	public function isInSedo ($domain) {
		global $sm;
		$result=$sm->Query("SELECT sedo FROM My_Domains WHERE url='$domain'");
		$Res=$sm->Fetch($result);
		if ($Res['sedo']=='1')
			return _('Yes');
		else 
			return _('No');
		
	}
	
	private function activateDomain ($domain) {
		global $sm;
		$result=$sm->Query("UPDATE My_Domains SET activo=1 WHERE url='$domain'");
		
	}
}

?>